<!-- Copyright 2010 John L. Reilly

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.   -->


<%@ page import="java.io.IOException"%>
<%@ page import="java.util.Collections"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.ListIterator"%>

<%@ page import="java.util.logging.Logger"%>

<%@ page import="javax.cache.Cache"%>
<%@ page import="javax.cache.CacheException"%>
<%@ page import="javax.cache.CacheManager"%>
<%@ page import="javax.servlet.ServletException"%>
<%@ page import="javax.servlet.http.HttpServlet"%>
<%@ page import="javax.servlet.http.HttpServletRequest"%>
<%@ page import="javax.servlet.http.HttpServletResponse"%>

<%@ page import="com.riq.entity.Alert"%>
<%@ page import="com.riq.entity.Department"%>
<%@ page import="com.riq.entity.Member"%>
<%@ page import="com.riq.entity.Location"%>
<%@ page import="com.riq.nonpersist.ReportDept"%>
<%@ page import="com.riq.nonpersist.ReportLocation"%>

<%

Alert a = (Alert) request.getAttribute("alert");
System.out.println("Alert msg: " + a.getalertMsgString());

String intent = (String) request.getAttribute("intent");
System.out.println("Intent: " + intent);

String path = (String) request.getAttribute("path");
System.out.println("Path: " + path);

List<ReportDept> allDepts = (List<ReportDept>) request.getAttribute("reportDepts");
System.out.println("Depts qty: " + allDepts.size());

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>FirstResponder</title>

<script type="text/javascript">

function detectScrollbar() {
  if (navigator.appName == "Microsoft Internet Explorer") {
    window.name=document.body.scrollTop;
  } else {
    window.name=window.pageYOffset;
  }
}

function doScroll() {
   if (window.name) window.scrollTo(0, window.name);
}

function toggle1(showHideDiv, switchTextDiv) {
	//toggles the div to hidden
  var ele = document.getElementById(showHideDiv);
  var text = document.getElementById(switchTextDiv);
  if(ele.style.display == "block") {
     ele.style.display = "none";
     text.innerHTML = "show";  
  } else {
    ele.style.display = "block";
    ele.style.display = "none";
    text.innerHTML = "show";
  }
}

function toggle2(showHideDiv, switchTextDiv) {
	// toggles the div, switches the header text, saves state in cookie
  var ele = document.getElementById(showHideDiv);
  var text = document.getElementById(switchTextDiv);
  if(ele.style.display == "block") {
        ele.style.display = "none";
    text.innerHTML = "show";
    setCookie(showHideDiv, 'closed', 7);
    }
  else {
    ele.style.display = "block";
    text.innerHTML = "hide";
    setCookie(showHideDiv, 'open', 7);
  }
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1) {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

</script>

<% 
Cookie[] cookie = request.getCookies(); 
%>

<!--META-->
<meta http-equiv="content-type"  content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width" />

<!--STYLESHEET-->
<link rel="stylesheet" type="text/css" href="stylesheets/static.css" />
</head>

<body>
<!--FIXED HEADERS-->
<div class="headwrap">
<div class="hBlack">
<div class="headerLeftTop">FirstResponder</div>
<div class="headerRightTop">ver 0.9</div>
<div class="headerLeftBottom">Add Resources</div>
<div class="headerRightBottom">12:34 PM</div>
</div>
<div class="hGray">
  <a class="buttonGrayHdr gSelected" href="mainMenu.html">Main</a>
  <a class="buttonGrayHdr" href="commandMenu.html">Command</a>
  <a class="buttonGrayHdr" href="department?action=display">Admin</a>
</div>
</div>

<div class="outwrap">
<div class="headerPlugShort"></div>

<div class="gold upper" ><%=a.getalertMsgString() %></div>

<div id="centeredButtons"><ul><li>
<a class="button blue wide tall"
   href="reportingPrep?intent=<%=intent%>&alertId=<%=a.getid().toString()%>&deptId=<%=allDepts.get(0).dept().getid() %>">
   Cancel / Back</a></li></ul></div>
   
<div id="centeredButtons"><ul><li>
<a class="button green wide tall last" 
   href=" #" onclick="javascript:document.forms[0].submit();return false;">
   Add Selected Resources</a></li></ul></div>
 
<form action="/resources?action=add&intent=<%=intent%>" method="post">
<input type=hidden name="alertId" value="<%=a.getid()%>"></input>
<input type=hidden name="deptId" value="<%=allDepts.get(0).dept().getid()%>"></input>
<input type=hidden name="intent" value="<%=intent%>"></input>

<%

String closeType = "init";

for (ReportDept d : allDepts) {
	
    String tempLocationType = "init";
    System.out.println("Inside Dept Loop in parResourcesMulti"); %>

	<div class="sub gold" style="text-align: left; width: 80%; height: 60px; 
	border-top: solid black 1px;
	font-size: 16px; padding: 12px 10px 8px"><%=d.dept().getshortName()%>
	</div>
	
	<a class="sub subB button" style="clear:none; background-color: goldenrod; 
	border: 0px solid black; text-align: right; width: 20%; height: 60px; color:black;
	border-top: solid black 1px" 
	id="deptHdr<%=d.dept().getid()%>"  
	href="javascript:toggle2('deptCon<%=d.dept().getid()%>', 'deptHdr<%=d.dept().getid()%>');" >hide</a>
	<div id="deptCon<%=d.dept().getid()%>" style="display: block;">
	
	<div class="sub black">Inactive Locations</div>
	 
	<%	
	if (d.locations() != null) {
    for (String locationType : d.locTypes()) {
      
      // locationtype subhead%>
      <div class="sub black" style="clear: none; text-align: left; 
           width: 60%"><%=locationType%>
      </div><% 
       
      // save button %>
      <div><a class="sub green buttonright" style="clear:none; text-align: right; width: 20%" 
      href="#" onclick="javascript:document.forms[0].submit();return false;">Save</a>
      </div><% 
      
      // show/hide button for locationType subhead %>
      <a class="sub blue buttonright" style="clear:none; border: 0px solid black; 
          text-align: right; width: 20%; color: white; text-align: right" 
          id="hdr<%=d.dept().getid() %>|<%=locationType%>"  
          href="javascript:toggle2('con<%=d.dept().getid() %>|<%=locationType%>', 'hdr<%=d.dept().getid() %>|<%=locationType%>');" >hide</a>
      <div id="con<%=d.dept().getid() %>|<%=locationType%> %>" style="display: block;">  <%

 		for (ReportLocation l : d.locations()) { // reportlocation loop
 		  if (l.location().gettype().equals(locationType)) {

		  // individual Location details
		  %> 
		  <label for="<%=l.location().getid() %>|loc">
		  <div class="row" style="width: 100%">
		  <div class="form-field">
		      <input id="<%=l.location().getid() %>|loc" type="checkbox" name="locationId" 
		      value="<%=l.location().getid() %>"/>
		      <label for="<%=l.location().getid() %>|loc"><%=l.location().getshortName()%></label>
		  </div>
		  </div>
		  </label>  
	 
		  <%
		  // this section for hides Location Type details upon start for easier navigation %>
		  </div>
		  <script type="text/javascript" language="JavaScript">
		   toggle1('locCon<%=d.dept().getid()%>|<%=l.location().gettype()%>', 'locHdr<%=d.dept().getid()%>|<%=l.location().gettype()%>');
		  </script>
		  <% 
		  }
		} // clean reportlocation loop
		
    }
	} // clean locations loop
 
	// MEMBERS 
		
	%>
	<div class="sub black">Inactive Members</div>	
	<%
  for (String mType : d.memTypes()) { // mType loop
    if (d.inactiveMembers() != null) { %>
		  <label>
		    <div class="sub white" style="clear:left; width: 60%; padding: 14px 10px 2px 10px">
		    <%=mType%>
		    </div>
		  </label>
		  <label>
		  <a class="sub green button" style="clear:none; color: white; text-align: right; width: 20%" 
		  href="#" onclick="javascript:document.forms[0].submit();return false;">Add</a>
		  </label>
		  <label>
		  <a class="sub blue buttonright" style="clear:none; border: 0px solid black; 
		      text-align: right; width: 20%; color: white; text-align: right" id="memHdr<%=d.dept().getid() %>|<%=mType%>"  
		     href="javascript:toggle2('memCon<%=d.dept().getid()%>|<%=mType%>', 'memHdr<%=d.dept().getid()%>|<%=mType%>');" >hide</a>
		  </label>
		  <div id="memCon<%=d.dept().getid()%>|<%=mType %>" style="display: block;">  
		  
		  <%
		  for (Member m : d.inactiveMembers()) {
		    if (m.gettype().equals(mType)) {
			  // individual Member details %>
			  <label for="<%=m.getid() %>|loc">
			  <div class="row" style="width: 100%">
			  <div class="form-field">
			      <input id="<%=m.getid() %>|loc" type="checkbox" name="memberId" 
			      value="<%=m.getid() %>"/>
			      <label for="<%=m.getid() %>|loc"><%=m.getfirstName()%> <%=m.getlastName()%></label>
			  </div></div>
			  </label>
			 <% } 
			 }
		  

	  %>
	  </div> 
	  <script type="text/javascript" language="JavaScript">
	  toggle1('memCon<%=d.dept().getid()%>|<%=mType%>', 'memHdr<%=d.dept().getid()%>|<%=mType%>');
	  </script>
	  <%
		
    }    
  } // clean mType loop
   // this section hides Member Type details upon start for easier navigation
   
	%> 
	</div>
	<%

	// this section for hiding and showing Department details
	for (int i=0; i < cookie.length; i++) {
	  if (("deptCon" + d.dept().getid()).equalsIgnoreCase(cookie[i].getName())) {
	    if ("closed".equals(cookie[i].getValue())) {
	      // set div style to invisible and text to "show"
	      %>
	      <script type="text/javascript" language="JavaScript">
	      toggle2('deptCon<%=d.dept().getid()%>', 'deptHdr<%=d.dept().getid()%>');
	      </script>
	      <%
	    }
	  }
	} // clean dept hiding loop

} // clean dept loop


%>
</form>
    
<div class="hints">Quick Hints:</div>
<div class="reportWrapper">
Select one or more Members and/or Locations to add to this Alert.  
Note: once resources have been added they cannot be deleted from the Alert.
</div>

<!--FOOTER-->
<div class="footer">FirstResponder</div>

<!--OUTLINE WRAPPER -->
</div> 

</body>
</html>

